package com.highveldsky.leaverequest.client.ui;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.highveldsky.leaverequest.client.service.LeaveRequestService;
import com.highveldsky.leaverequest.client.service.LeaveRequestServiceAsync;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class LeaveRequestEntryPoint implements EntryPoint {

	private final LeaveRequestServiceAsync lrService = GWT.create(LeaveRequestService.class);	
	private NewLeaveRequestPanel newLeaveRequestPanel = new NewLeaveRequestPanel(lrService);
	private ApproveLeaveRequestsPanel approveLeaveRequestsPanel = new ApproveLeaveRequestsPanel(lrService);
    private DockLayoutPanel dockPanel = new DockLayoutPanel(Unit.EM);
    private FlowPanel centerPanel = new FlowPanel();
    private FlowPanel westPanel = new FlowPanel();
    private FlowPanel northPanel = new FlowPanel();
    private FlowPanel southPanel = new FlowPanel();

	public void onModuleLoad() {
		dockPanel.addStyleName("leave-request-dock-panel");
		dockPanel.addNorth(northPanel, 10);
		dockPanel.addWest(westPanel, 15);
		dockPanel.addSouth(southPanel, 5);
		dockPanel.add(centerPanel);
		initHeader();
		initFooter();
		initWestNav();		
		initCenter();		
	    RootLayoutPanel rp = RootLayoutPanel.get();
	    rp.add(dockPanel);	
	}
	
	private void initCenter() {
		centerPanel.addStyleName("leave-request-panel");
	}

	private void initWestNav() {
		Anchor newLeaveRequest = new Anchor("New Leave Request");
		Anchor approveLeaveRequests = new Anchor("Approve Leave Requests");
		newLeaveRequest.addClickHandler(new NewLeaveRequest());
		approveLeaveRequests.addClickHandler(new ApproveLeaveRequests());
		westPanel.addStyleName("leave-request-panel");
		westPanel.addStyleName("leave-request-west-panel");
		westPanel.add(newLeaveRequest);
		westPanel.add(approveLeaveRequests);
	}

	private void initFooter() {
		HTML header = new HTML("Footer");
		header.addStyleName("leave-request-footer");
		southPanel.add(header);
		southPanel.addStyleName("leave-request-panel");
		southPanel.addStyleName("leave-request-south-panel");
	}

	private void initHeader() {
		HTML header = new HTML("Leave Request");
		header.addStyleName("leave-request-header-text");
		northPanel.add(header);
		northPanel.addStyleName("leave-request-panel");
		northPanel.addStyleName("leave-request-header-panel");
	}

	class NewLeaveRequest implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			centerPanel.clear();
			centerPanel.add(newLeaveRequestPanel);
		}
	}
	
	class ApproveLeaveRequests implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			centerPanel.clear();
			centerPanel.add(approveLeaveRequestsPanel);
		}
	}
}
